/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.catalina;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;

/**
 * <p>A <b>Valve</b> is a request processing component associated with a
 * particular Container.  A series of Valves are generally associated with
 * each other into a Pipeline.  The detailed contract for a Valve is included
 * in the description of the <code>invoke()</code> method below.</p>
 *
 * <b>HISTORICAL NOTE</b>:  The "Valve" name was assigned to this concept
 * because a valve is what you use in a real world pipeline to control and/or
 * modify flows through it.
 *
 * @author Craig R. McClanahan
 * @author Gunnar Rjnning
 * @author Peter Donald
 */
// Valve是与特定容器相关联的请求处理组件。
// 一系列阀门通常相互关联成一个管道。
// Valve的详细契约包含在下面invoke()方法的描述中。
// 历史注意:“阀门”这个名字被分配给这个概念，因为阀门是你在现实世界的管道中用来控制和/或修改流经它的流量的。
public interface Valve {


    //-------------------------------------------------------------- Properties

    /**
     * @return the next Valve in the pipeline containing this Valve, if any.
     */
    // 返回: 管道中包含该阀门的下一个阀门(如果有的话)。
    Valve getNext();


    /**
     * Set the next Valve in the pipeline containing this Valve.
     *
     * @param valve The new next valve, or <code>null</code> if none
     */
    // 在管道中设置包含此阀门的下一个阀门。
    // 参数: valve -新的下一个阀门，如果没有则为null
    void setNext(Valve valve);


    //---------------------------------------------------------- Public Methods


    /**
     * Execute a periodic task, such as reloading, etc. This method will be
     * invoked inside the classloading context of this container. Unexpected
     * throwables will be caught and logged.
     */
    // 执行周期性任务，如重新加载等。
    // 此方法将在此容器的类加载上下文中调用。
    // 意外的抛掷将被捕获并记录。
    void backgroundProcess();


    /**
     * <p>Perform request processing as required by this Valve.</p>
     *
     * <p>An individual Valve <b>MAY</b> perform the following actions, in
     * the specified order:</p>
     * <ul>
     * <li>Examine and/or modify the properties of the specified Request and
     *     Response.
     * <li>Examine the properties of the specified Request, completely generate
     *     the corresponding Response, and return control to the caller.
     * <li>Examine the properties of the specified Request and Response, wrap
     *     either or both of these objects to supplement their functionality,
     *     and pass them on.
     * <li>If the corresponding Response was not generated (and control was not
     *     returned, call the next Valve in the pipeline (if there is one) by
     *     executing <code>getNext().invoke()</code>.
     * <li>Examine, but not modify, the properties of the resulting Response
     *     (which was created by a subsequently invoked Valve or Container).
     * </ul>
     *
     * <p>A Valve <b>MUST NOT</b> do any of the following things:</p>
     * <ul>
     * <li>Change request properties that have already been used to direct
     *     the flow of processing control for this request (for instance,
     *     trying to change the virtual host to which a Request should be
     *     sent from a pipeline attached to a Host or Context in the
     *     standard implementation).
     * <li>Create a completed Response <strong>AND</strong> pass this
     *     Request and Response on to the next Valve in the pipeline.
     * <li>Consume bytes from the input stream associated with the Request,
     *     unless it is completely generating the response, or wrapping the
     *     request before passing it on.
     * <li>Modify the HTTP headers included with the Response after the
     *     <code>getNext().invoke()</code> method has returned.
     * <li>Perform any actions on the output stream associated with the
     *     specified Response after the <code>getNext().invoke()</code> method has
     *     returned.
     * </ul>
     *
     * @param request The servlet request to be processed
     * @param response The servlet response to be created
     *
     * @exception IOException if an input/output error occurs, or is thrown
     *  by a subsequently invoked Valve, Filter, or Servlet
     * @exception ServletException if a servlet error occurs, or is thrown
     *  by a subsequently invoked Valve, Filter, or Servlet
     */
    // 执行此阀门要求的请求处理。
    // 单个阀门可以按照指定的顺序执行以下动作:
    // 检查和/或修改指定的请求和响应的属性。
    // 检查指定Request的属性，完全生成相应的Response，并将控制权返回给调用者。
    // 检查指定的Request和Response的属性，包装其中一个或两个对象以补充它们的功能，并将它们传递下去。
    // 如果没有生成相应的Response(并且没有返回控件)，则通过执行getNext().invoke()调用管道中的下一个Valve(如果有的话)。
    // 检查(但不修改)结果响应(由随后调用的Valve或Container创建)的属性。
    // 阀门绝对不能做以下任何事情:
    // 更改已经用于指导此请求的处理控制流的请求属性(例如，尝试更改应该从附加到标准实现中的主机或上下文的管道发送请求的虚拟主机)。
    // 创建一个完整的响应，并将此请求和响应传递给管道中的下一个阀门。
    // 使用与请求相关的输入流中的字节，除非它完全生成响应，或者在传递请求之前包装请求。
    // 在getNext().invoke()方法返回后，修改包含在响应中的HTTP报头。
    // 在getNext().invoke()方法返回后，对与指定响应关联的输出流执行任何操作。
    // 参数: request——要处理的servlet请求
    // response -要创建的servlet响应
    // 抛出: IOException——如果发生输入/输出错误，或者由随后调用的Valve、Filter或Servlet抛出
    // ServletException——如果发生了servlet错误，或者由随后调用的Valve、Filter或servlet抛出
    void invoke(Request request, Response response)
        throws IOException, ServletException;

    // 是否支持异步
    boolean isAsyncSupported();
}
